{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dotenv import load_dotenv\n",
    "from agents import Agent, Runner, trace, function_tool\n",
    "from openai.types.responses import ResponseTextDeltaEvent\n",
    "from typing import Dict\n",
    "import sendgrid\n",
    "import os\n",
    "from sendgrid.helpers.mail import Mail, Email, To, Content\n",
    "import asyncio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "load_dotenv(override=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "instruction_seb_1 = \"\"\"You are information security proffesional expert. \n",
    "    Your goal is to perform DEEP analysis if provided link is secure and can be \n",
    "    accessed by user safely. \n",
    "    Open provided link and check if domain and page content are save.\n",
    "    Check if webpage is malicous and if its purpuse is to cheat people  \n",
    "    forcing them to provide personal information such as address, credit card etc. \n",
    "    Access 3 known security sites to see if there is already some feedback available about the url or its domain.\n",
    "    Please recommend with links best sites that can verify if the url is save\"\"\"\n",
    "\n",
    "instruction_seb_2 = \"\"\"You are information security proffesional expert. \n",
    "    Your goal is to perform BASIC analysis if provided link is secure and can be \n",
    "    accessed by user safely. \n",
    "    Open provided link and check if domain and page content are save.\n",
    "    Check if webpage is malicous and if its purpuse is to cheat people  \n",
    "    forcing them to provide personal information such as address, credit card etc. \n",
    "    Access 1 known security site to see if there is already some feedback available about the url or its domain.\n",
    "    Please recommend with links best sites that can verify if the url is save\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "security_agent_seb_1 = Agent(\n",
    "    name=\"Deep proffessional expert\", \n",
    "    instructions=instruction_seb_1,\n",
    "    model=\"gpt-4o-mini\"\n",
    "    )\n",
    "\n",
    "security_agent_seb_2 = Agent(\n",
    "    name=\"Basic proffessional expert\", \n",
    "    instructions=instruction_seb_1,\n",
    "    model=\"gpt-4o-mini\"\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "description = \"Check if link is valid from security perspective\"\n",
    "\n",
    "securitycheck_1 = security_agent_seb_1.as_tool(tool_name=\"professional_checker\",\n",
    "   tool_description=description\n",
    "   )\n",
    "securitycheck_2 = security_agent_seb_2.as_tool(tool_name=\"basic_checker\",\n",
    "   tool_description=description\n",
    "   )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "@function_tool\n",
    "def send_html_email(subject: str, html_body: str) -> Dict[str, str]:\n",
    "    \"\"\" Send out an email with the given subject and HTML body\"\"\"\n",
    "    sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))\n",
    "    from_email = Email(\"wieczoreks@hotmail.com\")  # Change to your verified sender\n",
    "    to_email = To(\"wieczorekseb@gmail.com\")  # Change to your recipient\n",
    "    content = Content(\"text/html\", html_body)\n",
    "    mail = Mail(from_email, to_email, subject, content).get()\n",
    "    sg.client.mail.send.post(request_body=mail)\n",
    "    return {\"status\": \"success\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "subject_instructions = \"You can write email subject from a given email body (in the field of IT software security feedback)\"\n",
    "subject_writer = Agent(\n",
    "    name=\"subject_writer\",\n",
    "    instructions=subject_instructions, \n",
    "    model=\"gpt-4o-mini\")\n",
    "subject_writer_tool = subject_writer.as_tool(\n",
    "    tool_name=\"subject_writer_tool\",\n",
    "    tool_description=\"Write a subject for email for security check on url\")\n",
    "\n",
    "html_formatter_instructions =\"You can convert given email body text about url security feedback into HTML email body\\\n",
    "    Your goal is to convert given email body text to simple nice looking html email layout \\\n",
    "        Make sure on top of the email, below greeting to write link to investigated url\"\n",
    "html_formatter=Agent(\n",
    "    name=\"html_formatter_tool\",\n",
    "    instructions=html_formatter_instructions,\n",
    "    model=\"gpt-4o-mini\"\n",
    "    )\n",
    "html_formatter_tool=html_formatter.as_tool(\n",
    "    tool_name=\"html_formatter_tool\",\n",
    "    tool_description=\"Convert a text email body to an HTML email body\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "email_tools = [subject_writer_tool, html_formatter_tool, send_html_email]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "email_creation_instructions = \"You are an email formatter and sender.\\\n",
    "    You receive body of the email (security feedback abou url) to be sent.\\\n",
    "    You first use subject_writer_tool to write email subject then use \\\n",
    "    html_formatter_tool to convert the body to HTML.\\\n",
    "    and you use the send_html_email tool to send the email with the subject and HTML body formatted for email.\\\n",
    "    Make sure not to send pure HTML in the email that when received looks like a HTML code instead of formattd email\"\n",
    "email_agent = Agent(\n",
    "    name=\"Email Manager\", \n",
    "    instructions=email_creation_instructions,\n",
    "    model=\"gpt-4o-mini\",\n",
    "    tools=email_tools,\n",
    "    handoff_description=\"Convert an email to HTML and send it\"\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "url= \"https://www.reddit.com/r/okbuddycinephile/comments/1otx0q8/favorite_actor_or_actress_who_found_success/\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "security_tools = [securitycheck_1, securitycheck_2]\n",
    "handoff_mailer = [email_agent]  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "security_manager_instructions = \"\"\"You are security manager in the field of IT software security\n",
    "      Follow these steps carefuly:\n",
    "      1. Generate two security feedbacks about provided url_1 \n",
    "        Use security_tools: securitycheck_1 & securitycheck_2 tools to generate 2 different security feedbacks about provided url\n",
    "      2. Evaluate and select\n",
    "        Review feedbacks and choose single best feedback using your judgement of which one is most effective.\n",
    "        You can use the tools only once.\n",
    "      3. Handoff for Sending\n",
    "        Pass only winning feedback to the 'Email Manager' agent\n",
    "Crucial Rules:\n",
    "- You must use the security_tools tools to generate feedbacks — do not write them yourself.\n",
    "- You must hand off exactly ONE feedback security text to the Email Manager — never more than one.\n",
    "\"\"\"\n",
    "\n",
    "security_manager = Agent(\n",
    "    name=\"Security Manager\",\n",
    "    instructions=security_manager_instructions,\n",
    "    model=\"gpt-4o-mini\",\n",
    "    tools=security_tools,\n",
    "    handoffs=handoff_mailer\n",
    ")\n",
    "\n",
    "message=\"Pick best security advise about url:\"+ url + \" and send email with security feedback\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "with trace(\"Pick best security advise about url and send email\"):\n",
    "    result = await Runner.run(security_manager, message)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
